package edu.whu.puppygo.assistant;

import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static java.time.Duration.ofSeconds;


/**
 * @author guanruwang
 * @date 2023/11/15$
 */
@Configuration
public class AssistantSupport {

    private static final String apiKey = "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";

    private static final String baseUrl = "https://openai-proxy-qlxsgoebao.ap-southeast-1.fcapp.run/v1";

    @Bean
    PersistentChatMemoryStore memoryStore() {
        return new PersistentChatMemoryStore();
    }

    /**
     * @implNote 使用阿里云函数做中间代理
     */
    @Bean
    ChatLanguageModel chatLanguageModel() {


        return OpenAiChatModel.builder()
                .apiKey(apiKey)
                .timeout(ofSeconds(120))
                .baseUrl(baseUrl)
                .logRequests(true)
                .logResponses(true)
                .build();
    }

    @Bean
    ChatMemoryProvider chatMemoryProvider(PersistentChatMemoryStore memoryStore) {
        ChatMemoryProvider chatMemoryProvider = memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(10)
                .chatMemoryStore(memoryStore)
                .build();
        return chatMemoryProvider;
    }

    @Bean
    HealthAssistant customerSupportAgent(ChatLanguageModel chatLanguageModel, HealthAssistantTools healthAssistantTools, ChatMemoryProvider chatMemoryProvider
    ) {
        return AiServices.builder(HealthAssistant.class)
                .chatLanguageModel(chatLanguageModel)
                .chatMemoryProvider(chatMemoryProvider)
                .tools(healthAssistantTools)
                .build();
    }

    @Bean
    ClassificationAssistant classificationAssistant(ChatLanguageModel chatLanguageModel, ChatMemoryProvider chatMemoryProvider
    ) {
        return AiServices.builder(ClassificationAssistant.class)
                .chatLanguageModel(chatLanguageModel)
                .build();
    }


}
